import {
  getFormValues,
  getSubmitFormValues,
} from "@ey-utils/utils/formily-utils/getSubmitFormValues";
import { useLocation as location } from "@umijs/max";

import { useContext, useEffect, useCallback } from 'react';
import { UNSAFE_NavigationContext as NavigationContext } from 'react-router-dom';





const useLocation = () => {
  const { search } = location();
  const query: { [key: string]: any } = search
      .substring(1)
      .split("&")
      .reduce((pre, cur) => {
        const [key, value] = cur.split("=");
        pre[key] = value;
        return pre;
      }, {});
  return { query: query };
};


const useBlocker = (blocker:any, when = true) => {
  const { navigator } = useContext(NavigationContext);
  useEffect(() => {
    if (!when) {
      return;
    }
    const unblock = navigator.block((tx:any) => {
      const autoUnblockingTx = {
        ...tx,
        retry() {
          unblock();
          tx.retry();
        },
      };
      blocker(autoUnblockingTx);
    });
  }, [navigator, blocker, when]);
};




const getUserBeforehand = () => {

}

const getUserLoanBill = () => {

}

export {
  getUserBeforehand,
  getUserLoanBill,
  getFormValues,
  getSubmitFormValues,
  useLocation,
};
